package com.jiantong.lawEnforcing.impl.service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.lawEnforcing.dto.CasesCheckDto;
import com.jiantong.lawEnforcing.impl.dao.CasesBasicDao;
import com.jiantong.lawEnforcing.impl.dao.CasesCheckDao;
import com.jiantong.lawEnforcing.impl.dao.PartiesInformationDao;
import com.jiantong.lawEnforcing.model.CasesBasic;
import com.jiantong.lawEnforcing.model.CasesCheck;
import com.jiantong.lawEnforcing.model.PartiesInformation;
import com.jiantong.lawEnforcing.service.CasesCheckService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.common.utils.JsonMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 立案审批
 * Created by mz03 on 2017/8/14.
 */
@Slf4j
@Service
@RpcProvider
public class CasesCheckServiceImpl implements CasesCheckService {
    private final CasesCheckDao casesCheckDao;
    private final CasesBasicDao casesBasicDao;
    private final PartiesInformationDao partiesInformationDao;
    private static final ObjectMapper objectMapper= JsonMapper.nonEmptyMapper().getMapper();

    @Autowired
    public CasesCheckServiceImpl(CasesCheckDao casesCheckDao, CasesBasicDao casesBasicDao, PartiesInformationDao partiesInformationDao) {
        this.casesCheckDao = casesCheckDao;
        this.casesBasicDao = casesBasicDao;
        this.partiesInformationDao = partiesInformationDao;
    }

    /**
     * id 查找
     *
     * @param id
     * @return casesCheck
     */
    @Override
    public Response<CasesCheck> finById(Long id) {
        try {
            CasesCheck casesCheck = casesCheckDao.findById(id);
            if (casesCheck != null) {
                return Response.ok(casesCheck);
            } else {
                log.error("casesCheck (id={}) is not found", id);
                return Response.fail("casesCheck.not.found");
            }
        }catch (Exception e){
            log.error("failed to find casesCheck(id={}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("casesCheck.find.fail");
        }
    }

    /**
     * id 查找CasesCheckDto
     *
     * @param id
     * @return News
     */
    @Override
    public Response<CasesCheckDto> findCasesCheckDtoById(Long id) {
        try {
            CasesCheck casesCheck = finById(id).getResult();
            CasesBasic casesBasic = casesBasicDao.findById(casesCheck.getCases_basic_id());
            if (casesBasic == null ){
                log.error("casesBasic (id={}) is not found", casesCheck.getCases_basic_id());
                return Response.fail("casesBasic.not.found");
            }
            PartiesInformation partiesInformation = partiesInformationDao.findById(casesBasic.getParties_id());
            if (partiesInformation == null){
                log.error("partiesInformation (id={}) is not found", casesBasic.getParties_id());
                return Response.fail("partiesInformation.not.found");
            }
            CasesCheckDto casesCheckDto = new CasesCheckDto();
            casesCheckDto.setCasesCheck(casesCheck);
            casesCheckDto.setCasesBasic(casesBasic);
            casesCheckDto.setPartiesInformation(partiesInformation);
            return Response.ok(casesCheckDto);
        }catch (Exception e){
            log.error("casesCheckDto (id={}) is not found", id);
            return Response.fail("casesCheckDto.not.found");
        }
    }

    /**
     * cases_basic_id 查找CasesCheckDto
     *
     * @param cases_basic_id
     * @return News
     */
    @Override
    public Response<CasesCheckDto> findByCases_basic_id(Long cases_basic_id) {
        try{
            Map<String,Object> map = Maps.newHashMap();
            if (cases_basic_id != null){
                map.put("cases_basic_id",cases_basic_id);
            }

            List<CasesCheck> list =  casesCheckDao.list(map);
            if (list.size()==0){
                log.error("CasesCheck (cases_basic_id={}) is not found",cases_basic_id);
                return Response.fail("CasesCheck.not.found");
            }

            CasesCheck casesCheck = list.get(0);
            CasesBasic casesBasic = casesBasicDao.findById(casesCheck.getCases_basic_id());
            if (casesBasic == null){
                log.error("casesBasic (id={}) is not found", casesCheck.getCases_basic_id());
                return Response.fail("casesBasic.not.found");
            }

            PartiesInformation partiesInformation = partiesInformationDao.findById(casesBasic.getParties_id());
            if (partiesInformation == null){
                log.error("partiesInformation (id={}) is not found", casesBasic.getParties_id());
                return Response.fail("partiesInformation.not.found");
            }
            CasesCheckDto casesCheckDto = new CasesCheckDto();
            casesCheckDto.setCasesCheck(casesCheck);
            casesCheckDto.setCasesBasic(casesBasic);
            casesCheckDto.setPartiesInformation(partiesInformation);
            return Response.ok(casesCheckDto);
        }catch(Exception e){
            log.error("failed to find casesCheckDto(cases_basic_id={}),cause:{}",cases_basic_id, Throwables.getStackTraceAsString(e));
            return Response.fail("simplenessCasesDto.find.fail");
        }
    }

    /**
     * 创建
     *
     * @param casesCheck 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Long> create(CasesCheck casesCheck) {
        try {
            casesCheckDao.create(casesCheck);
            return Response.ok(casesCheck.getId());
        }catch (Exception e) {
            log.error("failed.to.create.casesCheck:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("create.casesCheck.failed");
        }
    }

    /**
     * 更新
     *
     * @param casesCheck 单据对象
     * @return true or false
     */
    @Override
    public Response<Boolean> update(CasesCheck casesCheck) {
        try{
            return Response.ok(casesCheckDao.update(casesCheck));
        }catch (Exception e){
            log.error("failed.to.update.casesCheck:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("update.casesCheck.failed");
        }
    }

    /**
     * 根据状态分页查询
     *
     * @param pageNo
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public Response<Paging<CasesCheck>> pagingByStatus(
            Integer pageNo,
            Integer pageSize,
            List<Integer> status,
            Integer is_succeed,
            String cause_action,
            Integer cases_source,
            Integer parties_type,
            String parties_name
    ) {
        try{

            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String, Object> criteria = Maps.newHashMap();

            if(status != null){
                criteria.put("status_list",status);
            }
            if(is_succeed != null){
                criteria.put("is_succeed",is_succeed);
            }
            if(cause_action != null){
                criteria.put("cause_action",cause_action);
            }
            if(cases_source != null){
                criteria.put("cases_source",cases_source);
            }
            if(parties_type != null){
                criteria.put("parties_type",parties_type);
            }
            if(parties_name != null){
                criteria.put("parties_name",parties_name);
            }
            return Response.ok(casesCheckDao.pagingList(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to find CasesCheck(status={}), cause:{}", status, Throwables.getStackTraceAsString(e));
            return Response.fail("CasesCheck.find.fail");
        }
    }

    /**
     * 根据状态分页查询 DTO
     *
     * @param pageNo
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public Response<Paging<CasesCheckDto>> pagingCasesCheckDto(
            Integer pageNo,
            Integer pageSize,
            List<Integer> status,
            Integer is_succeed,
            String cause_action,
            Integer cases_source,
            Integer parties_type,
            String parties_name
    ) {
        try{

            Response<Paging<CasesCheck>> response = pagingByStatus(pageNo,pageSize,status,is_succeed,cause_action,cases_source,parties_type,parties_name);
            List<CasesCheckDto> casesCheckDtoList = new ArrayList<>();
            for (CasesCheck casesCheck:response.getResult().getData()) {
                CasesBasic casesBasic = casesBasicDao.findById(casesCheck.getCases_basic_id());
                if (casesBasic == null){
                    log.error("failed to find casesBasic(id={})", casesCheck.getCases_basic_id());
                    return Response.fail("casesBasic.find.fail");
                }
                PartiesInformation partiesInformation = partiesInformationDao.findById(casesBasic.getParties_id());
                if (partiesInformation == null){
                    log.error("failed to find partiesInformation(id={})", casesBasic.getParties_id());
                    return Response.fail("partiesInformation.find.fail");
                }
                CasesCheckDto casesCheckDto = new CasesCheckDto();
                casesCheckDto.setPartiesInformation(partiesInformation);
                casesCheckDto.setCasesBasic(casesBasic);
                casesCheckDto.setCasesCheck(casesCheck);
                casesCheckDtoList.add(casesCheckDto);
            }
            return Response.ok(new Paging<>(response.getResult().getTotal(),casesCheckDtoList));
        }catch (Exception e){
            log.error("failed to find CasesCheckDto(status={}), cause:{}", status, Throwables.getStackTraceAsString(e));
            return Response.fail("CasesCheckDto.find.fail");
        }

    }
}
